//
//  GXShopIntroduceController.m
//  YIChuSeller
//
//  Created by 田小椿 on 2018/11/27.
//  Copyright © 2018年 xiangshengjun. All rights reserved.
//

#import "GXShopIntroduceController.h"

@interface GXShopIntroduceController ()<UITextViewDelegate>
@property (nonatomic,strong) UILabel * placeHolderLabel;
@property (nonatomic,strong) UITextView * myTextView;
@property (nonatomic,strong) UILabel * limitLabel;
@property (nonatomic,strong) UIButton * saveBtn;
@end

@implementation GXShopIntroduceController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"店铺简介";
    [self creatAllView];
    [self setUpNavigationRightBarButtonItem];
}
- (void)creatAllView
{
    self.view.backgroundColor = kGlobalbgColor;
    [self.view addSubview:self.myTextView];
    [self.myTextView addSubview:self.placeHolderLabel];
    [self.view addSubview:self.limitLabel];
    [self.view addSubview:self.saveBtn];
}
-(void)setUpNavigationRightBarButtonItem
{
    UIButton *rightBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.frame=CGRectMake(0,5,40,20);
    [rightBtn setTitle:@"清空" forState:UIControlStateNormal];
    [rightBtn setTintColor:[UIColor whiteColor]];
    rightBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    [rightBtn addTarget:self action:@selector(enterRightButton:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightBarBtn=[[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    self.navigationItem.rightBarButtonItem=rightBarBtn;
}
-(void)enterRightButton:(UIButton *)btn
{
    _myTextView.text = @"";
    _placeHolderLabel.hidden = NO;
    _limitLabel.text = @"0/200";
}
- (UILabel *)placeHolderLabel
{
    if (_placeHolderLabel == nil) {
        UILabel *placeHolderLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 260, 20)];
        placeHolderLabel.text = @"请输入店铺简介";
        placeHolderLabel.numberOfLines = 0;
        placeHolderLabel.textColor = [UIColor lightGrayColor];
        placeHolderLabel.font = [UIFont systemFontOfSize:13];
        [placeHolderLabel sizeToFit];
        _placeHolderLabel = placeHolderLabel;
    }
    return _placeHolderLabel;
}
- (UITextView *)myTextView
{
    if (_myTextView == nil) {
        _myTextView = [[UITextView alloc]initWithFrame:CGRectMake(10, 15, kUIScreenWidth-20, 200)];
        _myTextView.font = [UIFont systemFontOfSize:15];
        _myTextView.delegate = self;
        _myTextView.backgroundColor = [UIColor whiteColor];
        _myTextView.textColor=[UIColor lightGrayColor];
        _myTextView.layer.masksToBounds = YES;
        [_myTextView.layer setCornerRadius:5];
        _myTextView.textContainerInset = UIEdgeInsetsMake(10, 10, 10, 10);
    }
    return _myTextView;
}
- (UILabel *)limitLabel
{
    if (_limitLabel == nil) {
        _limitLabel = [[UILabel alloc]initWithFrame:CGRectMake(kUIScreenWidth-100-10, self.myTextView.height+20, 100, 15)];
        _limitLabel.textColor = [UIColor darkGrayColor];
        _limitLabel.font = [UIFont systemFontOfSize:16];
        _limitLabel.textAlignment = NSTextAlignmentRight;
        _limitLabel.text = @"0/200";
    }
    return _limitLabel;
         
}
- (UIButton *)saveBtn
{
    if (_saveBtn == nil) {
        _saveBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        _saveBtn.frame = CGRectMake(0, kUIScreenHeight-40-Height_NavBar, kUIScreenWidth,40);
        [_saveBtn setTitle:@"保存" forState:UIControlStateNormal];
        [_saveBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _saveBtn.backgroundColor = RGBColor(114, 216, 207);
        _saveBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        [_saveBtn addTarget:self action:@selector(clickSaveBtn:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _saveBtn;
}

- (void)clickSaveBtn:(UIButton *)saveBtn
{
    if ([NSString isBlankString:self.myTextView.text]) {
        [SVProgressHUD showErrorWithStatus:@"输入店铺简介为空"];
        return;
    }

    NSString * storeId = [AccountTool sharedAccountTool].account.sotres[@"storeId"];
    [ShopAuthenticationManager changeShopIntroduceMessageWithStoreID:storeId introduce:self.myTextView.text success:^(id responseObject) {
        if (responseObject) {
            if (self.changeStateBlock) {
                self.changeStateBlock(YES);
            }
        }
    } fail:^(id responseObject) {
    }];    
}



-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.myTextView resignFirstResponder];
}
#pragma UITextViewDelegate
- (void)textViewDidBeginEditing:(UITextView *)textView
{
    _placeHolderLabel.hidden = YES;
    [self.myTextView becomeFirstResponder];
}
- (void)textViewDidEndEditing:(UITextView *)textView
{
    if (textView.text.length <1) {
        _placeHolderLabel.hidden = NO;
    }else
    {
          _placeHolderLabel.hidden = YES;
    }
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    //判断加上输入的字符，是否超过界限
    NSString *str = [NSString stringWithFormat:@"%@%@", textView.text, text];
    if (str.length > 200)
    {
         NSRange rangeIndex = [str rangeOfComposedCharacterSequenceAtIndex:200];
        if (rangeIndex.length == 1)
        {
            
            textView.text = [str substringToIndex:200];
            //这里重新统计下字数，字数超限，我发现就不走textViewDidChange方法了，你若不统计字数，忽略这行
            self.limitLabel.text = [NSString stringWithFormat:@"%lu/%d", (unsigned long)textView.text.length, 200];
        }else{
            NSRange rangeRange = [str rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, 200)];
            textView.text = [str substringWithRange:rangeRange];
        }
        return NO;
    }
    return YES;
}
- (void)textViewDidChange:(UITextView *)textView
{
    if (textView.text.length > 200)
    {
        textView.text = [textView.text substringToIndex:200];
    }
    //记录输入的字数，你若不统计字数，忽略这行
    self.limitLabel.text = [NSString stringWithFormat:@"%lu/%d", (unsigned long)[textView.text length],200];
}

@end
